package com.hb.software.profit.impl;

import com.hb.software.entity.profit.Profit;
import com.hb.software.entity.profit.ProfitMessage;
import com.hb.software.profit.ProfitService;
import com.hb.software.service.impl.BaseServiceImpl;
import com.hb.software.profit.dao.ProfitDao;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ProfitServiceImpl extends BaseServiceImpl<ProfitMessage, Long> implements ProfitService {

    private static final Logger logger = LogManager.getLogger(ProfitServiceImpl.class.getName());

    private ProfitDao profitDao;

    @Autowired
    public void setWorkerDAO(ProfitDao profitDao) {
        this.profitDao = profitDao;
        this.dao = this.profitDao;
    }

    @Override
    public List findByName(String name) {
        List list = profitDao.findByName(name);
        return list;
    }

    @Override
    public List findByDate(String date) {
        List list = profitDao.findByDate(date);
        return list;
    }

    @Override
    public long findLastId() {
        try{
            return profitDao.findLastId();
        }catch (Exception e){
            return 0;
        }


    }


//    @Override
//    public List findByDepartment(String department) {
//        return workerDao.findByDepartment(department);
//    }

    @Override
    public String createExcel(List<ProfitMessage> list,String path) {

        // 创建一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建一个工作表
        HSSFSheet sheet = workbook.createSheet("业绩表");
        // 添加表头行
        HSSFRow hssfRow = sheet.createRow(0);
        // 设置单元格格式居中
        HSSFCellStyle cellStyle = workbook.createCellStyle();
//    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 添加表头内容
        HSSFCell headCell = hssfRow.createCell(0);
        headCell.setCellValue("编号");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(1);
        headCell.setCellValue("姓名");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(2);
        headCell.setCellValue("月份");
        headCell.setCellStyle(cellStyle);

        headCell = hssfRow.createCell(3);
        headCell.setCellValue("业绩");
        headCell.setCellStyle(cellStyle);


        // 添加数据内容
        for (int i = 0; i < list.size(); i++) {
            hssfRow = sheet.createRow((int) i + 1);
            ProfitMessage profit = list.get(i);

            // 创建单元格，并设置值
            HSSFCell cell = hssfRow.createCell(0);
            cell.setCellValue(profit.getId());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(1);
            cell.setCellValue(profit.getName());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(2);
            cell.setCellValue(profit.getDate());
            cell.setCellStyle(cellStyle);

            cell = hssfRow.createCell(3);
            cell.setCellValue(profit.getProfit());
            cell.setCellStyle(cellStyle);

        }

        // 保存Excel文件
        try {
            OutputStream outputStream = new FileOutputStream(path);
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            return "error";
        }
        return "success";
    }


}

